<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;

use App\Http\Controllers\Controller;

use App\Http\Requests;

use DB;

class addressController extends Controller
{
    //地址显示页面
    public function index()
    {
        // 查询所需字段并返回
        $list = DB::table('storeaddress')
            ->join('storeuser', 'storeuser.userId', '=', 'storeaddress.uid')
            ->select('storeaddress.*', 'storeuser.userName')
            ->where('storeuser.userName','=',session('user')->userName)
            ->paginate(3);
            
        return view('home.address',['list'=>$list]);
    }

    public function create()
    {
        return view('home.address');
    }

    public function store(Request $request)
    {
        // 获取传来的字段，再将其转换成数据库相对应字段
        $province = $request['city1'];
        $city = $request['city2'];
        $country = $request['city3'];
        $town = $request['city4'];
        
        $p = DB::table('district')
            ->select('name')
            ->where('id','=',$province)
            ->get();
         foreach ($p as $key => $value) {
            $pp = $value;
            foreach ($pp as $key => $v) {
                $province = $v;
            }
        }
        $c = DB::table('district')
            ->select('name')
            ->where('id','=',$city)
            ->get();
         foreach ($c as $key => $value) {
            $cc = $value;
            foreach ($cc as $key => $v) {
                $city = $v;
            }
        }

        $cc = DB::table('district')
            ->select('name')
            ->where('id','=',$country)
            ->get();
        foreach ($cc as $key => $value) {
            $ccc = $value;
            foreach ($ccc as $key => $v) {
                $country = $v;
            }
        }

        $t = DB::table('district')
            ->select('name')
            ->where('id','=',$town)
            ->get();
        foreach ($t as $key => $value) {
            $tt = $value;
            foreach ($tt as $key => $v) {
                $town = $v;
            }
        }

        // 去除不需要的字段
        $data = $request->except('_token','city1','city2','city3','city4');

        // 将遍历出来的字段存在数据库相对应的字段中，若为---请选择---，则存null
        $data['province'] = $province;
        $data['city'] = $city;
        if ($country == '---请选择---') {
            $data['country'] = '';
        }else{
            $data['country'] = $country;
        }
        
        if ($town == '---请选择---') {
            $data['town'] = '';
        }else{
            $data['town'] = $town;
        }

        // 获取当前登录的用户名，并存入数据库相对应的字段
        $data['uid'] = session('user')->userId;

        $row = DB::table('storeaddress')->insert($data);
        if($row>0){
            return redirect('/address')->with('msg','添加成功');
        }

        //自定义错误信息
        $message = [
            'required' => ':attribute 字段值是必填项',
            'digits' => ':attribute 字段值必须为数值且长度为11',
        ];
        //设定验证规则
        $this->validate($request, [
            'consigneePhone' => 'required|digits:11',
            'uid' => 'required',
            'province' => 'required',
            'city' => 'required',
            'detailAddress' => 'required',
            'consignee' => 'required',
        ],$message);
    }

    public function edit($id)
    {
        $value = DB::table('storeaddress')->where('id',$id)->first();
        return view('home.addressEdit',['v'=>$value]);
    }

    public function update($id,Request $request)
    {

        // 原理同添加
        $province = $request['city1'];
        $city = $request['city2'];
        $country = $request['city3'];
        $town = $request['city4'];

        $p = DB::table('district')
            ->select('name')
            ->where('id','=',$province)
            ->get();
         foreach ($p as $key => $value) {
            $pp = $value;
            foreach ($pp as $key => $v) {
                $province = $v;
            }
        }
        $c = DB::table('district')
            ->select('name')
            ->where('id','=',$city)
            ->get();
         foreach ($c as $key => $value) {
            $cc = $value;
            foreach ($cc as $key => $v) {
                $city = $v;
            }
        }

        $cc = DB::table('district')
            ->select('name')
            ->where('id','=',$country)
            ->get();
        foreach ($cc as $key => $value) {
            $ccc = $value;
            foreach ($ccc as $key => $v) {
                $country = $v;
            }
        }

        $t = DB::table('district')
            ->select('name')
            ->where('id','=',$town)
            ->get();
        foreach ($t as $key => $value) {
            $tt = $value;
            foreach ($tt as $key => $v) {
                $town = $v;
            }
        }

        $data = $request->except('_token','city1','city2','city3','city4','_method');
        $data['province'] = $province;
        $data['city'] = $city;
        if ($country == '---请选择---') {
            $data['country'] = '';
        }else{
            $data['country'] = $country;
        }
        
        if ($town == '---请选择---') {
            $data['town'] = '';
        }else{
            $data['town'] = $town;
        }
        //自定义错误信息
        $message = [
            'required' => ':attribute 字段值是必填项',
            'digits' => ':attribute 字段值必须为数值且长度为11',
        ];
        //设定验证规则
        $this->validate($request, [
            'consigneePhone' => 'required|digits:11',
            'detailAddress' => 'required',
            'consignee' => 'required',
            'province' => 'required',
            'city' => 'required',
        ],$message);
        // dd($data);
        $row = DB::table('storeaddress')->where('id',$id)->update($data);
        if($row>0){
            return redirect('/address')->with('msg','修改成功');
        }
    }

    public function destroy($id)
    {
        $row = DB::table('storeaddress')->where('id',$id)->delete();
        if($row>0){
            return redirect('/address')->with('msg','删除成功');
        }
    }

    public function doget(Request $request)
    {
        $list = DB::table('district')->where('upid',$request->input('upid'))->get();
        echo json_encode($list);
    }

    public function dopost(Request $request)
    {
        $list = DB::table('district')->where('upid',$request->input('upid'))->get();
        echo json_encode($list);
    }


}